package application;

import java.util.ArrayList;
import com.db4o.Db4oEmbedded;
import com.db4o.ObjectContainer;
import com.db4o.ObjectSet;

public class Chapter implements Comparable<Chapter>{
	

	public int Nr;
	public String Name;
	public String Description;
	public ArrayList<Scene> scenes;
	public String dbFile;
	
	public Chapter(int Nr,
			String Name,
			String Description,
			ArrayList<Scene> scenes){
		super();

		this.Nr = Nr;
		this.Name = Name;
		this.Description = Description;
		this.scenes = scenes;
	}
	
	public Chapter(){
		super();

		this.Name = "new chapter";
		this.Description = "this is a chapter of your story";
		this.scenes = new ArrayList<Scene>();
	}
	
	public void dbCreateChapter (String dbFile){
		int x = countChapters(dbFile) + 1;
		this.setNr(x);
		
		ObjectContainer db = Db4oEmbedded.openFile(Db4oEmbedded
		        .newConfiguration(), dbFile);
		
		try{

	        		db.store(this);


		}
		finally{
			db.close();
		}
	}

	public void dbEditChapter (String dbFile, Chapter c){
		ObjectContainer db = Db4oEmbedded.openFile(Db4oEmbedded
		        .newConfiguration(), dbFile);
		
		try{
			try{
				ObjectSet<Chapter> result = db
		        .queryByExample(this);
				
				while(result.hasNext()){
					Chapter found = result.next();
					System.out.println("found");
					db.delete(found);
				}
				System.out.println("UPDATE");
				this.setDescription(c.getDescription());
				this.setName(c.getName());
				this.setNr(c.getNr());
				this.setScenes(c.getScenes());
				db.store(this);
				
			}
			catch(IllegalStateException e){
				db.close();
			}

		}
		finally{
			db.close();
		}
	}

	
	
	public void dbDeleteChapter (String dbFile){
		ObjectContainer db = Db4oEmbedded.openFile(Db4oEmbedded
		        .newConfiguration(), dbFile);
		
		try{
			try{
				ObjectSet<Chapter> result = db
		        .queryByExample(Chapter.this);
				Chapter found = (Chapter) result.next();
				db.delete(found);
			}
			catch(IllegalStateException e){
				db.close();
			}

		}
		finally{
			db.close();
		}
	}
	
	public void dbSceneUpdate(String dbFile,int index, Scene s){
		ObjectContainer db = Db4oEmbedded.openFile(Db4oEmbedded
		        .newConfiguration(), dbFile);
		
		try{

			ObjectSet<Chapter> result=db.queryByExample(Chapter.class);
	        while(result.hasNext()) {
	        	
	        	Chapter tmp = (Chapter) result.next();
	        	if(this.toString().equals(tmp.toString())){
	        		this.scenes.set(index, s);
	        		db.store(this);
	        		db.delete(tmp);
	        	}
	        }
		}
		finally{
			db.close();
		}
	}
	
	public void dbSceneRemove(String dbFile,Scene s){
		ObjectContainer db = Db4oEmbedded.openFile(Db4oEmbedded
		        .newConfiguration(), dbFile);
		
		try{

			ObjectSet<Chapter> result=db.queryByExample(Chapter.class);
	        while(result.hasNext()) {
	        	
	        	Chapter tmp = (Chapter) result.next();
	        	if(this.toString().equals(tmp.toString())){
	        		this.scenes.remove(s);
	        		ArrayList<Scene>scenes = this.getScenes();
	        		for(int i=0;i<scenes.size();i++){
	        			Scene sc = scenes.get(i);
	        			if(sc.getNr()>i+1){
	        				sc.setNr(i+1);
	        			}
	        			scenes.set(i, sc);
	        		}
	        		this.setScenes(scenes);
	        		db.store(this);
	        		db.delete(tmp);
	        	}
	        }
		}
		finally{
			db.close();
		}
	}
	public void dbSceneNrUp(String dbFile, Scene s){
		
		int x = s.getNr();
		int index = this.scenes.indexOf(s);
		try{
			
			if(x>1){
			for(int i=0;i<this.scenes.size();i++){
				Scene c = this.scenes.get(i);
				if (c.equals(s)){
					index = this.scenes.indexOf(c);
					c.setNr(x-1);
					this.scenes.set(index, c);
					Scene o = this.scenes.get(index-1);
					o.setNr(x);
					this.scenes.set(index-1, o);
				}
			}
		}

	}
	catch(java.lang.IndexOutOfBoundsException e){
		return;
	}
		
	}
	
	public void dbSceneNrDown(String dbFile, Scene s){
		int x = s.getNr();
		int index = this.scenes.indexOf(s);
		
		try{
			
			if(x<this.scenes.size()){
				for (int i=0;i<this.scenes.size();i++){
					Scene c = this.scenes.get(i);
					if (c.equals(s)){
						index = this.scenes.indexOf(c);
						c.setNr(x+1);
						this.scenes.set(index, c);
						Scene o = this.scenes.get(index+1);
						o.setNr(x);
						this.scenes.set(index+1, o);
					}
				}
			}

		}
		catch(java.lang.IndexOutOfBoundsException e){
			return;
		}
			
		}


	
	public void dbSceneCreateScene(String dbFile, Scene s){
		ObjectContainer db = Db4oEmbedded.openFile(Db4oEmbedded
		        .newConfiguration(), dbFile);
		
		try{
			s.setNr(countScenes(dbFile)+1);
			ObjectSet<Chapter> result=db.queryByExample(Chapter.class);
	        while(result.hasNext()) {
	        	
	        	Chapter tmp = (Chapter) result.next();
	        	if(this.toString().equals(tmp.toString())){
	        		this.scenes.add(s);
	        		db.store(this);
	        		db.delete(tmp);
	        	}
	        }
		}
		finally{
			db.close();
		}
	}

	
	public int countChapters(String dbFile){
		int x = 0;
		ObjectContainer db = Db4oEmbedded.openFile(Db4oEmbedded
		        .newConfiguration(), dbFile);
		try {
			
			ArrayList<Chapter> list= new ArrayList<Chapter>();
			ObjectSet<Chapter> result = db.queryByExample(Chapter.class);
		    while(result.hasNext()){
		    	Chapter tmp = (Chapter) result.next();
		    	list.add(tmp);
		    }
		    
		    x=list.size();   
		}
		finally{
			db.close();
		}
		db.close();
		return x;
	}
	public int countScenes(String dbFile){
		
		int x = this.getScenes().size();
		return x;

	}

    @Override
    public int compareTo(Chapter o) {
      return ((Integer)Nr).compareTo((Integer)o.Nr);
    }

	///////////////////
	// Getter & Setter:
	///////////////////
    
	public int getNr() {
		return Nr;
	}

	public String getName() {
		return Name;
	}

	public String getDescription() {
		return Description;
	}

	public void setNr(int nr) {
		Nr = nr;
	}

	public void setName(String name) {
		Name = name;
	}

	public void setDescription(String description) {
		Description = description;
	}

	
	public String toString() {
		return Nr + ". " + Name;
	}

	public ArrayList<Scene> getScenes() {
		return scenes;
	}

	public void setScenes(ArrayList<Scene> scenes) {
		this.scenes = scenes;
	}

}
